
class Population {
	Individual[]pop;
	Selection selection;
	
	public Population(int size,Individual prototype,Selection selection){
			pop=new Individual [size];
			
			for (int i=0;i<size;i=i+1){
				pop[i] = ((Individual)prototype.replicate());
				pop[i].initGenome();
   				//sorted(pop,i,proby.copy());	
			}
			sort(pop);
			this.selection=selection;
	}
	  private void sort(Comparable[] arr){
        boolean isSorted=false;
        Comparable tmp;
        for (int i=0;!isSorted&&i<arr.length-1;i++){
            isSorted=true;
            for(int j=0; j<arr.length-1-i; j++){
                if(arr[j].compareTo(arr[j+1])>0){
                    tmp=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=tmp;
                    isSorted=false;
                }
            }
        }
    }
/*	private void sorted(Individual[]pop,int i,Individual prototype){
		Individual pro;
		int j=0;
		if (i==0)
			 pop[i]=prototype;
		else{
		while  (j<i){
  			if (pop[j].compareTo(prototype)>0){
				pro=pop[j];
				pop[j]=prototype;
				Individual tu;
				for (int k=j+1;k<pop.length; k=k+1){
					tu=pop[k];
					pop[k]=pro;
					pro=tu;					
				}
				}
				j=j+1;
		}
		if(i==j)
			pop[i]=prototype;
				}
		}*/
public Individual getBest(){
	return pop[0];
}
public void nextGeneration(){
	Individual[] pop2=new Individual[this.pop.length];
	for(int i=0;i<this.pop.length;i=i+1){
		pop2[i]=selection.reproduce(this.pop);
			//sorted(pop2,i,temp);
	}
	sort(pop2);
	this.pop=pop2;
	
}	
	
}
